﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FinalProject.Services.Interfaces;
using FinalProject.Entities;

namespace FinalProject.Web.Controllers
{
    public class ChargeCategoryController : Controller
    {
        private readonly IChargeCategoryService _chargeCategoryService;

        public ChargeCategoryController(IChargeCategoryService chargeCategoryService)
        {
            this._chargeCategoryService = chargeCategoryService;
        }

        [HttpGet]
        public ActionResult Details(int? id = 0)
        {
            try
            {
                var chargeCategoryDetails = _chargeCategoryService.GetById((int)id);
                if (chargeCategoryDetails == null)
                    throw new ArgumentNullException("Not Found");
                return View(chargeCategoryDetails);
            }
            catch (Exception ex)
            {
                var model = new HandleErrorInfo(ex, "ChargeCategory", "Details");
                return View("Error", model);
            }
        }

        [HttpGet]
        public ActionResult Delete(int? id = 0)
        {
            try
            {
                var chargeCategoryDetails = _chargeCategoryService.GetById((int)id);
                if (chargeCategoryDetails == null)
                    throw new ArgumentNullException("Not Found");
                return View(chargeCategoryDetails);
            }
            catch (Exception ex)
            {
                var model = new HandleErrorInfo(ex, "ChargeCategory", "Delete");
                return View("Error", model);
            }
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult Delete(ChargeCategory chargeCategory)
        {
            _chargeCategoryService.Delete(chargeCategory.Id);
            return RedirectToAction("List", "ChargeCategory");
        }

        [HttpGet]
        public ActionResult Edit(int? id = 0)
        {
            try
            {
                var details = _chargeCategoryService.GetById((int)id);
                if (details == null)
                    throw new ArgumentNullException("Not Found");
                return View(details);
            }
            catch (Exception ex)
            {
                var model = new HandleErrorInfo(ex, "ChargeCategory", "Edit");
                return View("Error", model);
            }
        }

        [HttpPost]
        public ActionResult Edit(ChargeCategory chargeCategory)
        {
            _chargeCategoryService.Update(chargeCategory);
            return RedirectToAction("List", "ChargeCategory");
        }

        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(ChargeCategory chargeCategory)
        {
            _chargeCategoryService.Create(chargeCategory);
            return RedirectToAction("List", "ChargeCategory");
        }

        [HttpGet]
        public ActionResult List()
        {
            var list = _chargeCategoryService.GetAll().Entities;
            if (list.Any())
                return View("List", list);
            return View("List");
        }
    }
}
